﻿using D.AT.Dto;
using D.AT.Extensions;
using D.AT.HttpUtil;
using D.AT.Models;
using D.AT.UIUitl;

namespace D.AT
{
    public partial class FrmOcap : Form
    {
        public FrmOcap()
        {
            InitializeComponent();
            this.dgvOcap.SetGridDataViewStyle();
        }

        private async void FrmOcap_Load(object sender, EventArgs e)
        {
            LoadColumn();
            await LoadEnvAsync();
        }

        private List<EnvSettingEntity> envSettingEntities = null;
        private async Task LoadEnvAsync()
        {
            envSettingEntities = await OrmFactory.MySqllite.Select<EnvSettingEntity>().ToListAsync();
            this.cboEnv.DisplayMember = "Name";
            this.cboEnv.DataSource = envSettingEntities;
        }

        /// <summary>
        /// 加载列
        /// </summary>
        private void LoadColumn()
        {
            DataGridViewTextBoxColumn typeBoxColumn = new DataGridViewTextBoxColumn();
            typeBoxColumn.ReadOnly = true;
            typeBoxColumn.Name = "Type";
            typeBoxColumn.HeaderText = "Type";
            typeBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            typeBoxColumn.DataPropertyName = "Type";
            dgvOcap.Columns.Add(typeBoxColumn);

            DataGridViewTextBoxColumn valueBoxColumn = new DataGridViewTextBoxColumn();
            valueBoxColumn.ReadOnly = false;
            valueBoxColumn.Name = "Name";
            valueBoxColumn.HeaderText = "Name";
            valueBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            valueBoxColumn.DataPropertyName = "Name";
            dgvOcap.Columns.Add(valueBoxColumn);


            DataGridViewTextBoxColumn flowCodeBoxColumn = new DataGridViewTextBoxColumn();
            flowCodeBoxColumn.ReadOnly = false;
            flowCodeBoxColumn.Name = "FlowCode";
            flowCodeBoxColumn.HeaderText = "FlowCode";
            flowCodeBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            flowCodeBoxColumn.DataPropertyName = "FlowCode";
            dgvOcap.Columns.Add(flowCodeBoxColumn);

        }
        private List<OcapFlowList> ocapFlowLists;
        private async void btnGet_Click(object sender, EventArgs e)
        {
            var envSettingEntity = this.cboEnv.SelectedItem as EnvSettingEntity;
            if (envSettingEntity == null)
            {
                MessageBox.Show("请选择环境！"); return;
            }
            if (string.IsNullOrEmpty(envSettingEntity.OCAPIp) || string.IsNullOrEmpty(envSettingEntity.OCAPPort))
            {
                MessageBox.Show("暂未配置OCAP环境！"); return;
            }

            var url = $"http://{envSettingEntity.OCAPIp}:{envSettingEntity.OCAPPort}/api/iray-ocap/template";
            var headers = new Dictionary<string, string>();
            headers.Add("x-access-secret", envSettingEntity.XAccessSecret);
            var res = await HttpClientHelper.HttpGetAsync<GetOcapTemplateListResponseDto>(url, ServiceLocator.TimeOut, null, headers);
            if (res != null && res.data?.ocapFlowList != null)
            {
                ocapFlowLists = res.data.ocapFlowList;
                this.dgvOcap.DataSource = null;
                this.dgvOcap.DataSource = ocapFlowLists;

            }
            else
            {
                MessageBox.Show("获取失败");
            }
        }

        private async void btnAddCache_Click(object sender, EventArgs e)
        {
            var envSettingEntity = this.cboEnv.SelectedItem as EnvSettingEntity;
            if (envSettingEntity == null)
            {
                MessageBox.Show("请选择环境！"); return;
            }
            if (ocapFlowLists != null)
            {
                var list = new List<OCAPTemplateEntity>();
                foreach (var item in ocapFlowLists)
                {
                    list.Add(new OCAPTemplateEntity()
                    {
                        FlowCode = item.flowCode,
                        Lds = item.type,
                        Name = item.name,
                        EnvName = envSettingEntity.Name
                    });
                }

                await OrmFactory.MySqllite.Delete<OCAPTemplateEntity>().ExecuteAffrowsAsync();
                var res = await OrmFactory.MySqllite.Insert<OCAPTemplateEntity>(list).ExecuteAffrowsAsync();
                if (res>0)
                {
                    MessageBox.Show("缓存成功");
                }
                else
                {
                    MessageBox.Show("缓存失败");
                }
            }
        }
    }
}
